package com.cskaoyan.controller;
import com.cskaoyan.bean.CskaoyanmallCart;
import com.cskaoyan.bean.CskaoyanmallUser;
import com.cskaoyan.bean.bo.CartCheckedBo;
import com.cskaoyan.bean.bo.CartCheckoutBo;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.CartCheckeoutVo;
import com.cskaoyan.bean.vo.CartIndexVo;
import com.cskaoyan.mapper.CskaoyanmallUserMapper;
import com.cskaoyan.service.WXShoppingService;
import com.cskaoyan.utils.QueryUserIdByUserNameUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class WXShoppingCartController {
    @Autowired
    WXShoppingService wxShoppingService;
    @Autowired
    CskaoyanmallUserMapper userMapper;

    @RequestMapping("wx/cart/index")
    //购物车初始化页面
    public BaseRespVo cartIndex() {
        CskaoyanmallUser user = getUser();
        CartIndexVo cartIndexVo=wxShoppingService.cartIndex(user);
        return BaseRespVo.success(cartIndexVo);
    }
    @RequestMapping("/wx/cart/checked")
    public BaseRespVo cartChecked(@RequestBody CartCheckedBo bo) {
        CskaoyanmallUser user = getUser();
        CartIndexVo cartIndexVo = wxShoppingService.cartCheckedService(bo, user);
        return BaseRespVo.success(cartIndexVo);
    }
    @RequiresPermissions("wx:cart:add")
    @RequestMapping("wx/cart/add")
    public BaseRespVo cartAdd(@RequestBody CskaoyanmallCart bo) {
        CskaoyanmallUser user = getUser();
        Long count = wxShoppingService.cartAddService(bo, user);
        return BaseRespVo.success(count);
    }

    @RequestMapping("wx/cart/update")
    public BaseRespVo cartUpdate(@RequestBody CskaoyanmallCart bo) {
        CskaoyanmallUser user = getUser();
        int i =wxShoppingService.cartUpdateService(bo ,user);
        if (i != 0) {
            return BaseRespVo.success();
        }
        return BaseRespVo.fail(401);
    }

    @RequestMapping("wx/cart/delete")
    public BaseRespVo cartDelete( @RequestBody Map<String,List<Integer> >productIds) {
        CskaoyanmallUser user = getUser();
        System.out.println(productIds);
        int i = wxShoppingService.cartDeleteService(productIds, user);
        if (i != 0) {
            return BaseRespVo.success();
        }
        return BaseRespVo.fail(401);

    }

    @RequestMapping("wx/cart/goodscount")
    public BaseRespVo cartGoodscount() {
        CskaoyanmallUser user = getUser();
       int total = wxShoppingService.cartGoodscountService(user);
        return BaseRespVo.success(total);
    }

    @RequestMapping("wx/cart/fastadd")
    public BaseRespVo cartFastadd(@RequestBody CskaoyanmallCart bo) {
        CskaoyanmallUser user = getUser();
        Integer i= wxShoppingService.cartFastaddService(bo, user);
        return BaseRespVo.success(i);
    }


    @RequestMapping("wx/cart/checkout")
    public BaseRespVo cartCheckout( CartCheckoutBo bo) {
        CskaoyanmallUser user = getUser();
       CartCheckeoutVo checkeoutVo = wxShoppingService.cartCheckoutService(bo, user);
        return BaseRespVo.success(checkeoutVo);
    }

    //获取用户信息
    private CskaoyanmallUser getUser() {
        Subject subject = SecurityUtils.getSubject();
        Integer id = QueryUserIdByUserNameUtils.queryName(userMapper);
        CskaoyanmallUser user = new CskaoyanmallUser();
        return user;
    }
}
